/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package notificationcenter;

import company.search.qualifiers.RecruitEvent;
import company.search.qualifiers.VisitCandidateEvent;
import entities.Candidate;
import entities.CompanyAdquiredProfile;
import entities.CompanyVisitedProfile;
import entities.Notification;
import entities.NotificationType;
import facades.NotificationFacade;
import facades.NotificationTypeFacade;
import facades.UserFacade;
import java.util.Date;
import java.util.Locale;
import java.util.Random;
import java.util.ResourceBundle;
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.event.Event;
import javax.enterprise.event.Observes;
import javax.enterprise.event.TransactionPhase;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;
import org.primefaces.push.PushContext;
import org.primefaces.push.PushContextFactory;
import qualifiers.CreatedEntityEvent;
import qualifiers.NotificationEmail;
import qualifiers.PreCreateEntityEvent;
import qualifiers.ReportedProfile;
import qualifiers.ThrowNotification;
import util.DateConverter;

/**
 *
 * @author Alvaro
 */
@Named(value = "NotificationsCenter")
@ApplicationScoped
public class NotificationsCenter {

    @Inject
    UserFacade userFacade;
    @Inject
    NotificationFacade notificationFacade;
    @Inject
    NotificationTypeFacade notificationTypeFacade;
    @Inject
    @PreCreateEntityEvent
    Event<Notification> precreatedNotificationEvent;
    @Inject
    @CreatedEntityEvent
    Event<Notification> createdNotificationEvent;
    @Inject
    @ThrowNotification
    Event<Notification> throwNotificationEvent;
    @Inject
    @NotificationEmail
    Event<Notification> notificationEmail;
    private Random randomGenerator;
    /**
     * Creates a new instance of NotificationsCenter
     */
    public NotificationsCenter() {
        randomGenerator= new Random();
    }
    public void onNewCompanyAdquiredProfile(@Observes @RecruitEvent  CompanyAdquiredProfile companyAdquiredProfile){
        StringBuilder mensajeNotificacion=new StringBuilder();
        if(companyAdquiredProfile.getCompany().getCompanyConfiguration().getShowName()){
            mensajeNotificacion.append(companyAdquiredProfile.getCompany().getName());
        }
        else{
            mensajeNotificacion.append(getMessages().getString("NoNameCompany"));
        }
        mensajeNotificacion.append(" ");
        mensajeNotificacion.append(getMessages().getString("ProfileSelected"));
        mensajeNotificacion.append(" ");
        mensajeNotificacion.append(DateConverter.convertDateToString(new Date()));
        
        Notification notification=new Notification();
        notification.setIsNotificated(false);
        notification.setDescription(mensajeNotificacion.toString());
        notification.setDate(new Date());
        notification.setUserSender(companyAdquiredProfile.getCompany().getIdUser());
        notification.setUser(companyAdquiredProfile.getCandidate().getUser());
        NotificationType notificationType= notificationTypeFacade.find(1);
        notification.setNotificationType(notificationType);
        notificationFacade.create(notification);
        createdNotificationEvent.fire(notification);
        throwNotificationEvent.fire(notification);
        if(notification.getUser().getCandidate().getCandidateConfiguration()==null){
            notificationEmail.fire(notification);
        }
        else if(notification.getUser().getCandidate().getCandidateConfiguration().getEmailAdquired()){
            notificationEmail.fire(notification);
        }
        
    }
    
    public void onNewVisitedProfile(@Observes(during = TransactionPhase.AFTER_SUCCESS) @CreatedEntityEvent CompanyVisitedProfile companyVisitedProfile){
        StringBuilder mensajeNotificacion=new StringBuilder();
         if(companyVisitedProfile.getIdCompany().getCompanyConfiguration().getShowName()){
            mensajeNotificacion.append(companyVisitedProfile.getIdCompany().getName());
        }
        else{
            mensajeNotificacion.append(getMessages().getString("NoNameCompany"));
        }
        mensajeNotificacion.append(" ");
        mensajeNotificacion.append(getMessages().getString("ProfileVisited"));
        mensajeNotificacion.append(" ");
        mensajeNotificacion.append(DateConverter.convertDateToString(new Date()));
        
        Notification notification=new Notification();
        notification.setIsNotificated(false);
        notification.setDescription(mensajeNotificacion.toString());
        notification.setDate(new Date());
        notification.setUserSender(companyVisitedProfile.getIdCompany().getIdUser());
        notification.setUser(companyVisitedProfile.getIdCandidate().getUser());
        NotificationType notificationType= notificationTypeFacade.find(3);
        notification.setNotificationType(notificationType);
        notificationFacade.create(notification);
        throwNotificationEvent.fire(notification);
        if(notification.getUser().getCandidate().getCandidateConfiguration()==null){
            notificationEmail.fire(notification);
        }
        else if(notification.getUser().getCandidate().getCandidateConfiguration().getEmailVisited()){
            notificationEmail.fire(notification);
        }
    }
    
    
    public void onNewReportProfile(@Observes @ReportedProfile Candidate candidate){
        StringBuilder mensajeNotificacion=new StringBuilder();
        
        mensajeNotificacion.append(getMessages().getString("NotificationReportedProfile"));
        mensajeNotificacion.append(" ");
        mensajeNotificacion.append(DateConverter.convertDateToString(new Date()));
        
        Notification notification=new Notification();
        notification.setIsNotificated(false);
        notification.setDescription(mensajeNotificacion.toString());
        notification.setDate(new Date());
        notification.setUserSender(candidate.getUser());
        notification.setUser(candidate.getUser());
        NotificationType notificationType= notificationTypeFacade.find(4);
        notification.setNotificationType(notificationType);
        notificationFacade.create(notification);
        throwNotificationEvent.fire(notification);
        notificationEmail.fire(notification);
        
    }

    
    
    public void sendPush(@Observes(during = TransactionPhase.AFTER_SUCCESS) @ThrowNotification Notification notification){
        PushContext pushContext = PushContextFactory.getDefault().getPushContext();
        String channel="/notification/"+notification.getUser().getIdUser().toString();
        pushContext.push(channel, notification.getIdNotification().toString());
    }
    
    public ResourceBundle getMessages(){
        FacesContext context = FacesContext.getCurrentInstance();
        Locale locale=context.getViewRoot().getLocale();
        ResourceBundle rb = ResourceBundle.getBundle("messages", locale);
        return rb;
    }
    
}